home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / creator changer / creator changer project / source / util.java < prev   
Encoding:
Java Source  |  2000-06-23  |  4.0 KB  |  124 lines

  1. import java.awt.*;
  2. import java.io.*;
  3. import java.net.URL;
  4.  
  5. public class Util
  6. {
  7.     public static final Color GSBColor  = Color.black;
  8.     public static final Color GSWColor  = Color.white;
  9.     public static final Color GS1Color  = new Color(238, 238, 238);
  10.     public static final Color GS2Color  = new Color(221, 221, 221);
  11.     public static final Color GS3Color  = new Color(204, 204, 204);
  12.     public static final Color GS4Color  = new Color(187, 187, 187);
  13.     public static final Color GS5Color  = new Color(170, 170, 170);
  14.     public static final Color GS6Color  = new Color(153, 153, 153);
  15.     public static final Color GS7Color  = new Color(136, 136, 136);
  16.     public static final Color GS8Color  = new Color(119, 119, 119);
  17.     public static final Color GS9Color  = new Color(102, 102, 102);
  18.     public static final Color GS10Color = new Color( 85,  85,  85);
  19.     public static final Color GS11Color = new Color( 68,  68,  68);
  20.     public static final Color GS12Color = new Color( 34,  34,  34);
  21.     public static final Color GSA1Color = new Color( 51,  51,  51);
  22.     public static final Color GSA2Color = new Color( 17,  17,  17);
  23.  
  24.     /**
  25.      * Completely loads the Image referenced by the given filename.
  26.      * This will block until the image is loaded.
  27.      * @param filename the path of the Image to load.
  28.      * @param watcher the component to use to load the image.
  29.      * @return the loaded Image, or null if the loading fails.
  30.      */
  31.     public static Image loadImage(String filename, Component watcher)
  32.     {
  33.         Image image = null;
  34.         
  35.         if (filename != null)
  36.         {
  37.             URL url = watcher.getClass().getResource(filename);
  38.             if (url == null)
  39.             {
  40.                 System.err.println("loadImage() could not find \"" + filename + "\"");
  41.             }
  42.             else
  43.             {
  44.                 image = watcher.getToolkit().getImage(url);
  45.                 if (image == null)
  46.                 {
  47.                     System.err.println("loadImage() getImage() failed for \"" + filename + "\"");
  48.                 }
  49.                 else
  50.                 {
  51.                     MediaTracker tracker = new MediaTracker(watcher);
  52.         
  53.                     try
  54.                     {
  55.                         tracker.addImage(image, 0);
  56.                         tracker.waitForID(0);
  57.                     }
  58.                     catch (InterruptedException e) { System.err.println("loadImage(): " + e); }
  59.                     finally
  60.                     {
  61.                         boolean isError = tracker.isErrorAny();
  62.                         if (isError)
  63.                         {
  64.                             System.err.println("loadImage() failed to load \"" + filename + "\"");
  65.                             int flags = tracker.statusAll(true);
  66.         
  67.                             boolean loading = 0 != (flags & MediaTracker.LOADING);
  68.                             boolean aborted = 0 != (flags & MediaTracker.ABORTED);
  69.                             boolean errored = 0 != (flags & MediaTracker.ERRORED);
  70.                             boolean complete = 0 != (flags & MediaTracker.COMPLETE);
  71.                             System.err.println("loading: " + loading);
  72.                             System.err.println("aborted: " + aborted);
  73.                             System.err.println("errored: " + errored);
  74.                             System.err.println("complete: " + complete);
  75.                         }
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.         
  81.         return image;
  82.     }
  83.  
  84.     /**
  85.      * Reads in a text file to a String.
  86.      * There is probably a more elegant solution, but this works.
  87.      * @param file the text file to read.
  88.      * @return the string containing the contents of the file.
  89.      * @throws java.io.FileNotFoundException if the given file can not
  90.      * be found to be read.
  91.      */
  92.     public static String readTextResourceFile(String filePath, Object localRef) throws FileNotFoundException
  93.     {
  94.         String result = "";
  95.         
  96.         if (filePath != null && localRef != null)
  97.         {
  98.             InputStream inStream = localRef.getClass().getResourceAsStream(filePath);
  99.             if (inStream == null)
  100.                 throw new FileNotFoundException("Could not find \"" + filePath + "\"");
  101.                 
  102.             //Create a new buffered input stream out of the raw input stream, with a buffer of 8k.
  103.             BufferedInputStream in = new BufferedInputStream(inStream, 8192);
  104.             try
  105.             {
  106.                 StringBuffer sb = new StringBuffer();
  107.                 
  108.                 int c = in.read();
  109.                 while (c != -1)
  110.                 {
  111.                     sb = sb.append((char)c);
  112.                     c = in.read();
  113.                 }
  114.                 
  115.                 result = new String(sb);
  116.             }
  117.             catch (IOException exc)
  118.             {
  119.                 exc.printStackTrace();
  120.             }
  121.         }
  122.         return result;
  123.     }
  124. }